import Config from './config.js'
// import JSEncrypt from 'jsencrypt'
import Util from './util.js'
// import store from '@/store/index.js'
// const txIm = uni.requireNativePlugin('TX-TencentIM-Plus');
// #ifdef H5
// const app_type = Util.isWeiXin() ? 'wechat' : 'h5';
// const app_type_name = Util.isWeiXin() ? '微信公众号' : 'H5';
// #endif

// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

// #ifdef MP-ALIPAY
const app_type = 'aliapp';
const app_type_name = '支付宝小程序';
// #endif

// #ifdef MP-BAIDU
const app_type = 'baiduapp';
const app_type_name = '百度小程序';
// #endif

// #ifdef MP-TOUTIAO
const app_type = 'MP-TOUTIAO';
const app_type_name = '头条小程序';
// #endif

// #ifdef MP-QQ
const app_type = 'MP-QQ';
const app_type_name = 'QQ小程序';
// #endif

// #ifdef APP-PLUS
const app_type = 'app';
const app_type_name = 'APP';
// #endif

export default {
	sendRequest(params) {
		console.log(params)
		var method = params.method || 'POST', // 请求方式
			url = Config.baseUrl + params.url, // 请求路径
			data = [];
			var token='123'
			if(params.header && params.header.token){
				token=params.header.token;
			}
			if(uni.getStorageSync('token')){
				token=uni.getStorageSync('token')
			}
		// token
		// if (uni.getStorageSync('token')) data.token = uni.getStorageSync('token');

		// 门店id
		// if (uni.getStorageSync('store')) data.store_id = uni.getStorageSync('store').store_id;

		// 参数
		if (params.data != undefined) Object.assign(data, params.data);

		if (params.async === false) {
			//同步
			return new Promise((resolve, reject) => {
				uni.request({
					url: url,
					method: method,
					data:{
						"avatarUrl": data.avatarUrl,
						"city": data.city,
						"country": data.country,
						"gender": data.gender,
						"language": data.language,
						"nickName": data.nickName,
						"province": data.province
					},
					header:{
						'content-type': 'application/x-www-form-urlencoded',
						"Authorization":'Bearer challenger-eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzAyNTIwMjM4MyIsImV4cCI6MTcwMjAxNTk2MSwidXNlcmlkIjo0LCJpYXQiOjE3MDE5Mjk1NjF9.awbZWcRCOvWNEQjOkMT1rmeDrH1aestS0GbkNpH2-LWq4clLB9plqiEytGCieSOpEJYN8G7_MWniA9m9_n_lAw'
					},
					// dataType: params.dataType || 'json',
					// responseType: params.responseType || 'text',
					success: (res) => {
						if (res.data.code == -3 && store.state.siteState > 0) {
							// store.commit('setSiteState', -3)
							// Util.redirectTo('/otherpages/storeclose/storeclose/storeclose', {}, 'reLaunch');
							return;
						}
						if (res.data.refreshtoken) {
							uni.setStorage({
								key: 'token',
								data: res.data.refreshtoken
							});
						}
						if (res.data.code == -10009 || res.data.code == -10010) {
							uni.removeStorage({
								key: 'token'
							})
						}
						resolve(res.data);
					},
					fail: (res) => {
						reject(res);
					},
					complete: (res) => {
						reject(res);
					}
				});
			});
		} else {
			// console.log(params)
			// console.log(data)
			// console.log(url)
			//异步
			uni.request({
				url: url,
				method: method,
				data: data,
				header: {
					// 'content-type': 'application/x-www-form-urlencoded',
					"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
					// 'token':token,
					"Authorization":'Bearer challenger-eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzAyNTIwMjM4MyIsImV4cCI6MTcwMjAxNTk2MSwidXNlcmlkIjo0LCJpYXQiOjE3MDE5Mjk1NjF9.awbZWcRCOvWNEQjOkMT1rmeDrH1aestS0GbkNpH2-LWq4clLB9plqiEytGCieSOpEJYN8G7_MWniA9m9_n_lAw'
				},
				// dataType: params.dataType || 'json',
				responseType: 'arraybuffer',
				success: (res) => {
					console.log(res)
					if (res.data.code == 401) {
						uni.hideLoading()
						uni.clearStorageSync();
						// 	txIm.logout(result=>{
						// 					console.log(result);
						// 		uni.redirectTo({
						// 			url:"/pages/login/login"
						// 		})			
						// }) 
						// store.commit('setSiteState', -3)
						// Util.redirectTo('/otherpages/storeclose/storeclose/storeclose', {}, 'reLaunch');
						return;
					}
					if (res.data.refreshtoken) {
						uni.setStorage({
							key: 'token',
							data: res.data.refreshtoken
						});
					}
					if (res.data.code == -10009 || res.data.code == -10010) {
						uni.removeStorage({
							key: 'token'
						})
					}
					typeof params.success == 'function' && params.success(res.data);
				},
				fail: (res) => {
					typeof params.fail == 'function' && params.fail(res);
				},
				complete: (res) => {
					typeof params.complete == 'function' && params.complete(res);
				}
			});
		}
	}
}
